'use strict';

const dayjs = require("dayjs");

module.exports = app => {
  const { INTEGER, DATE, STRING } = app.Sequelize;

  const CourseRecord = app.model.define('CourseRecord', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    course_id: { type: INTEGER, allowNull: false, comment: '课程ID' },
    course_logo: { type: STRING(255), comment: '课程图片' },
    course_name: { type: STRING(100), allowNull: false, comment: '课程名称' },
    course_schedule: { type: STRING(100), allowNull: false, comment: '上课时间' },
    grade: { type: STRING(20), allowNull: false, comment: '年级' },
    class_name: { type: STRING(20), allowNull: false, comment: '班级' },
    teacher_name: { type: STRING(30), allowNull: false, comment: '上课老师' },
    location: { type: STRING(100), allowNull: false, comment: '上课地点' },
    parent_id: { type: INTEGER, allowNull: false, comment: '家长ID' },
    student_id: { type: INTEGER, allowNull: false, comment: '学生ID' },
    status: { type: INTEGER, defaultValue: 1, comment: '状态：1-正常，0-已取消' },
    create_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    updated_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    }
  }, {
    tableName: 'course_record',
    timestamps: true,
    updatedAt: 'updated_time',
    createdAt: 'create_time',
  });

  CourseRecord.associate = function() {
    // 关联课程
    app.model.CourseRecord.belongsTo(app.model.Course, {
      foreignKey: 'course_id',
      as: 'course'
    });
    // 关联学生
    app.model.CourseRecord.belongsTo(app.model.Student, {
      foreignKey: 'student_id',
      as: 'student'
    });
    app.model.CourseRecord.belongsTo(app.model.Parent, {
      foreignKey: 'parent_id',
      as: 'parent'
    });
  };

  return CourseRecord;
};
